<template>
  <div class="login-container">
    <div class="login-card">
      <div class="login-header">
        <h1>欢迎回来</h1>
        <p>登录您的账号，开始学习之旅</p>
      </div>

      <form @submit.prevent="handleLogin" class="login-form">
        <div class="form-group">
          <label for="username">用户名/邮箱</label>
          <div class="input-group">
            <i class="fas fa-user"></i>
            <input
              type="text"
              id="username"
              v-model="loginForm.username"
              placeholder="请输入用户名或邮箱"
              required
            />
          </div>
        </div>

        <div class="form-group">
          <label for="password">密码</label>
          <div class="input-group">
            <i class="fas fa-lock"></i>
            <input
              :type="showPassword ? 'text' : 'password'"
              id="password"
              v-model="loginForm.password"
              placeholder="请输入密码"
              required
            />
            <i
              class="fas"
              :class="showPassword ? 'fa-eye-slash' : 'fa-eye'"
              @click="togglePasswordVisibility"
            ></i>
          </div>
        </div>
        <div class="form-group">
          <label for="captchaCode">验证码</label>
          <div class="input-group">
            <el-input
              v-model="loginForm.captchaCode"
              auto-complete="off"
              size="large"
              class="flex-1"
              placeholder="请输入验证码"
            />
            <el-image
              :src="captchaBase64"
              class="captcha-img"
              @click="getCaptcha"
            />
          </div>
        </div>
        <div class="form-options">
          <label class="remember-me">
            <input type="checkbox" v-model="rememberMe" />
            <span>记住我</span>
          </label>
          <!-- <a href="#" class="forgot-password">忘记密码？</a> -->
        </div>

        <button type="submit" class="login-button" :disabled="loading">
          <span v-if="loading" class="loading-spinner"></span>
          {{ loading ? "登录中..." : "登录" }}
        </button>

        <div class="register-link">
          还没有账号？
          <router-link to="/register">立即注册</router-link>
        </div>
      </form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from "vue";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";
import type { LoginData } from "@/api/user";
import * as AuthAPI from "@/api/user";
import { useUserStore } from "@/store/modules/user";
const router = useRouter();
const showPassword = ref(false);
const rememberMe = ref(false);
const loading = ref(false);

const userStore = useUserStore();
const togglePasswordVisibility = () => {
  showPassword.value = !showPassword.value;
};
const loginForm = ref<LoginData>({
  username: "",
  password: "",
  captchaCode: "",
  captchaKey: "",
});
const handleLogin = async () => {
  if (
    !loginForm?.value?.username ||
    !loginForm?.value?.password ||
    !loginForm?.value?.captchaCode
  ) {
    ElMessage.warning("请输入用户名和密码");
    return;
  }

  try {
    loading.value = true;
    console.log(loginForm.value, "loginForm");

    // TODO: 调用登录API
    // const response = await login(username.value, password.value);

    userStore
      .login(loginForm.value)
      .then(async () => {
        await userStore.getUserInfo();
        // 登录后跳转到之前重定向的页面，如果没有跳到主页
        const redirect = router.currentRoute.value.query.redirect as string;
        if (redirect) {
          router.push(redirect);
        } else {
          router.push("/");
        }
      })
      .finally(() => {
        getCaptcha();
        loading.value = false;
      });
  } catch (error) {
  } finally {
    loading.value = false;
  }
};
const captchaBase64 = ref(); // 验证码图片Base64字符串
// 获取验证码
function getCaptcha() {
  AuthAPI.getCaptcha().then((data) => {
    loginForm.value.captchaKey = data.captchaKey;
    captchaBase64.value = data.captchaBase64;
  });
}
onMounted(() => {
  getCaptcha();
});
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #f8f9fa;
  padding: 20px;
}

.login-card {
  width: 100%;
  max-width: 400px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
  padding: 30px;
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h1 {
  font-size: 24px;
  color: #333;
  margin-bottom: 10px;
}

.login-header p {
  color: #666;
  font-size: 14px;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  color: #333;
  font-weight: 500;
  text-align: left;
  font-size: 14px;
}

.input-group {
  position: relative;
  display: flex;
  align-items: center;
}

.input-group i {
  position: absolute;
  left: 12px;
  color: #999;
  font-size: 16px;
}

.input-group input {
  width: 100%;
  padding: 10px 10px 10px 40px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 14px;
  transition: all 0.3s;
}

.input-group input:focus {
  border-color: #1890ff;
  box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.1);
  outline: none;
}

.input-group i:last-child {
  right: 12px;
  left: auto;
  cursor: pointer;
  color: #999;
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.remember-me {
  display: flex;
  align-items: center;
  gap: 6px;
  color: #666;
  cursor: pointer;
  font-size: 14px;
}

.remember-me input {
  width: 14px;
  height: 14px;
}

.forgot-password {
  color: #1890ff;
  text-decoration: none;
  font-size: 14px;
}

.forgot-password:hover {
  text-decoration: underline;
}
.captcha-img {
  height: 48px;
  cursor: pointer;
  border-top-right-radius: 6px;
  border-bottom-right-radius: 6px;
}
.login-button {
  width: 100%;
  padding: 10px;
  background-color: #1890ff;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 8px;
}

.login-button:hover {
  background-color: #40a9ff;
}

.login-button:disabled {
  background-color: #d9d9d9;
  cursor: not-allowed;
}

.loading-spinner {
  width: 16px;
  height: 16px;
  border: 2px solid white;
  border-radius: 50%;
  border-top-color: transparent;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.divider {
  display: flex;
  align-items: center;
  margin: 20px 0;
}

.divider::before,
.divider::after {
  content: "";
  flex: 1;
  height: 1px;
  background: #eee;
}

.divider span {
  padding: 0 15px;
  color: #999;
  font-size: 14px;
}

.social-login {
  display: flex;
  justify-content: center;
  gap: 20px;
  margin-bottom: 20px;
}

.social-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  font-size: 18px;
  color: white;
  cursor: pointer;
  transition: all 0.3s;
  display: flex;
  justify-content: center;
  align-items: center;
}

.social-btn.wechat {
  background-color: #07c160;
}

.social-btn.qq {
  background-color: #12b7f5;
}

.social-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.register-link {
  text-align: center;
  color: #666;
  font-size: 14px;
}

.register-link a {
  color: #1890ff;
  text-decoration: none;
  margin-left: 5px;
}

.register-link a:hover {
  text-decoration: underline;
}

@media (max-width: 480px) {
  .login-card {
    padding: 20px;
  }

  .login-header h1 {
    font-size: 20px;
  }

  .login-header p {
    font-size: 12px;
  }
}
</style>
